package com.vay.de.dang.online.view;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.vay.de.dang.online.R;
import com.vay.de.dang.online.VnApp;
import com.vay.de.dang.online.ui.HomeActivity;
import com.vay.de.dang.online.util.VnContext;
import com.vay.de.dang.online.web.LoginApi;
import com.vay.de.dang.online.web.ReqCallback;
import com.vay.de.dang.online.web.ReqResult;

import cc.taylorzhang.singleclick.SingleClickUtil;

public class DialogUnsubscribe extends DialogBase {

    static long MaxTime = 60 * 1000;
    private TextView hint0;
    private TextView bntSend;
    private EditText editText;
    private Handler handler = new Handler();
    private long lastTime;
    private Activity activity;
    private boolean requestCodeDoing = true;

    public DialogUnsubscribe(Context context) {
        super(context);
        setContentView(R.layout.dialog_del_account);
        activity = (Activity) context;
        findViewById(R.id.bnt_close).setOnClickListener(view -> {
            dismiss();
        });

        findViewById(R.id.bnt_ok).setOnClickListener(view -> {

            String phone = VnContext.instance().getPhone();
            String code = editText.getText().toString().trim();

            if (TextUtils.isEmpty(code)) {
                showText(getContext().getString(R.string.error_v_code));
                return;
            }

            LoginApi.deleteAccount(phone, code, new ReqCallback(activity) {

                @Override
                public void callback(ReqResult value) {
                    if (value.isOk()) {
                        VnContext.instance().logout();
                        HomeActivity.reOpen(activity);
                        activity.finish();
                    }
                }
            }.showDialog(true));
        });

        bntSend = findViewById(R.id.bnt_send);
        editText = findViewById(R.id.edit_code);
        hint0 = findViewById(R.id.hint_0);
        hint0.setText(VnApp.getStr(R.string.label_delete_account_1) + VnContext.instance().getPhone());

        SingleClickUtil.onSingleClick(bntSend, view -> {

            if (!requestCodeDoing) return;
            requestCodeDoing = false;

            String phone = VnContext.instance().getPhone();
            LoginApi.sendCode(phone, new ReqCallback(activity) {

                @Override
                public void end() {
                    super.end();
                    requestCodeDoing = true;
                }

                @Override
                public void callback(ReqResult value) {
                    if (value.isOk()) {

                        bntSend.setEnabled(false);
                        bntSend.setClickable(false);

                        lastTime = System.currentTimeMillis();
                        handler.postDelayed(timer, 0);
                    }
                }
            }.showDialog(true));
        });

    }

    private Runnable timer = new Runnable() {

        @Override
        public void run() {
            long time = MaxTime - (System.currentTimeMillis() - lastTime);
            if (time <= 0) {
                bntSend.setText(VnApp.getStr(R.string.bnt_send));
                bntSend.setClickable(true);
                bntSend.setEnabled(true);
            } else {
                bntSend.setText((time / 1000) + "s");
                handler.postDelayed(timer, 1000);
            }
        }
    };
}
